/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.fixedexpense;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.fixedexpense.CurrencyLadderPriceDao;
import com.jeesite.modules.base.dao.fixedexpense.CustomerPriceDao;
import com.jeesite.modules.base.dao.fixedexpense.FixedExpenseDao;
import com.jeesite.modules.base.dao.fixedexpense.LadderPriceDao;
import com.jeesite.modules.base.dao.fixedexpense.SupplierPriceDao;
import com.jeesite.modules.base.dao.fixedexpense.VehicleSettlementInfoDao;
import com.jeesite.modules.base.entity.fixedexpense.CurrencyLadderPrice;
import com.jeesite.modules.base.entity.fixedexpense.CustomerPrice;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.entity.fixedexpense.LadderPrice;
import com.jeesite.modules.base.entity.fixedexpense.SupplierPrice;
import com.jeesite.modules.base.entity.fixedexpense.VehicleSettlementInfo;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 固定费用项目Service
 * @author Kilo
 * @version 2021-03-02
 */
@Service
@Transactional(readOnly=true)
public class FixedExpenseService extends CrudService<FixedExpenseDao, FixedExpense> {
	
	@Autowired
	private CurrencyLadderPriceDao currencyLadderPriceDao;
	
	@Autowired
	private LadderPriceDao ladderPriceDao;
	
	@Autowired
	private CustomerPriceDao customerPriceDao;
	
	@Autowired
	private SupplierPriceDao supplierPriceDao;
	
	@Autowired
	private VehicleSettlementInfoDao vehicleSettlementInfoDao;

	@Autowired
	private CodeRuleService codeRuleService;

	/**
	 * 获取单条数据
	 * @param fixedExpense
	 * @return
	 */
	@Override
	public FixedExpense get(FixedExpense fixedExpense) {
		FixedExpense entity = super.get(fixedExpense);
		if (entity != null){
			CurrencyLadderPrice currencyLadderPrice = new CurrencyLadderPrice(entity);
			currencyLadderPrice.setStatus(CurrencyLadderPrice.STATUS_NORMAL);
			entity.setCurrencyLadderPriceList(currencyLadderPriceDao.findList(currencyLadderPrice));
			LadderPrice ladderPrice = new LadderPrice(entity);
			ladderPrice.setStatus(LadderPrice.STATUS_NORMAL);
			entity.setLadderPriceList(ladderPriceDao.findList(ladderPrice));
			CustomerPrice customerPrice = new CustomerPrice(entity);
			customerPrice.setStatus(CustomerPrice.STATUS_NORMAL);
			entity.setCustomerPriceList(customerPriceDao.findList(customerPrice));
			SupplierPrice supplierPrice = new SupplierPrice(entity);
			supplierPrice.setStatus(SupplierPrice.STATUS_NORMAL);
			entity.setSupplierPriceList(supplierPriceDao.findList(supplierPrice));
			VehicleSettlementInfo vehicleSettlementInfo = new VehicleSettlementInfo(entity);
			vehicleSettlementInfo.setStatus(VehicleSettlementInfo.STATUS_NORMAL);
			entity.setVehicleSettlementInfoList(vehicleSettlementInfoDao.findList(vehicleSettlementInfo));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param fixedExpense 查询条件
	 * @param fixedExpense.page 分页对象
	 * @return
	 */
	@Override
	public Page<FixedExpense> findPage(FixedExpense fixedExpense) {
		return super.findPage(fixedExpense);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param fixedExpense
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(FixedExpense fixedExpense) {
		if(fixedExpense.getIsNewRecord()){
			fixedExpense.setFixedExpenseId(codeRuleService.getCode("fixedcostpro"));
            fixedExpense.setCreateBy(UserUtils.getUser().getUserCode());
            fixedExpense.setUpdateBy(UserUtils.getUser().getUserCode());
        }else {
            fixedExpense.setUpdateBy(UserUtils.getUser().getUserCode());
        }
		super.save(fixedExpense);
		if (fixedExpense.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("fixedcostpro");
		}
		// 保存上传附件
		FileUploadUtils.saveFileUpload(fixedExpense.getId(), "fixedExpense_file");
		// 保存 FixedExpense子表
		for (CurrencyLadderPrice currencyLadderPrice : fixedExpense.getCurrencyLadderPriceList()){
			if (!CurrencyLadderPrice.STATUS_DELETE.equals(currencyLadderPrice.getStatus())){
				currencyLadderPrice.setFixedExpenseId(fixedExpense);
				if (currencyLadderPrice.getIsNewRecord()){
					currencyLadderPriceDao.insert(currencyLadderPrice);
				}else{
					currencyLadderPriceDao.update(currencyLadderPrice);
				}
			}else{
				currencyLadderPriceDao.delete(currencyLadderPrice);
			}
		}
		// 保存 FixedExpense子表
		for (LadderPrice ladderPrice : fixedExpense.getLadderPriceList()){
			if (!LadderPrice.STATUS_DELETE.equals(ladderPrice.getStatus())){
				ladderPrice.setFixedExpenseId(fixedExpense);
				if (ladderPrice.getIsNewRecord()){
					ladderPriceDao.insert(ladderPrice);
				}else{
					ladderPriceDao.update(ladderPrice);
				}
			}else{
				ladderPriceDao.delete(ladderPrice);
			}
		}
		// 保存 FixedExpense子表
		for (CustomerPrice customerPrice : fixedExpense.getCustomerPriceList()){
			if (!CustomerPrice.STATUS_DELETE.equals(customerPrice.getStatus())){
				customerPrice.setFixedExpenseId(fixedExpense);
				if (customerPrice.getIsNewRecord()){
					customerPriceDao.insert(customerPrice);
				}else{
					customerPriceDao.update(customerPrice);
				}
			}else{
				customerPriceDao.delete(customerPrice);
			}
		}
		// 保存 FixedExpense子表
		for (SupplierPrice supplierPrice : fixedExpense.getSupplierPriceList()){
			if (!SupplierPrice.STATUS_DELETE.equals(supplierPrice.getStatus())){
				supplierPrice.setFixedExpenseId(fixedExpense);
				if (supplierPrice.getIsNewRecord()){
					supplierPriceDao.insert(supplierPrice);
				}else{
					supplierPriceDao.update(supplierPrice);
				}
			}else{
				supplierPriceDao.delete(supplierPrice);
			}
		}
		// 保存 FixedExpense子表
		for (VehicleSettlementInfo vehicleSettlementInfo : fixedExpense.getVehicleSettlementInfoList()){
			if (!VehicleSettlementInfo.STATUS_DELETE.equals(vehicleSettlementInfo.getStatus())){
				vehicleSettlementInfo.setExpenseItemId(fixedExpense);
				if (vehicleSettlementInfo.getIsNewRecord()){
					vehicleSettlementInfoDao.insert(vehicleSettlementInfo);
				}else{
					vehicleSettlementInfoDao.update(vehicleSettlementInfo);
				}
			}else{
				vehicleSettlementInfoDao.delete(vehicleSettlementInfo);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param fixedExpense
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(FixedExpense fixedExpense) {
		super.updateStatus(fixedExpense);
	}
	
	/**
	 * 删除数据
	 * @param fixedExpense
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(FixedExpense fixedExpense) {
		super.delete(fixedExpense);
		CurrencyLadderPrice currencyLadderPrice = new CurrencyLadderPrice();
		currencyLadderPrice.setFixedExpenseId(fixedExpense);
		currencyLadderPriceDao.deleteByEntity(currencyLadderPrice);
		LadderPrice ladderPrice = new LadderPrice();
		ladderPrice.setFixedExpenseId(fixedExpense);
		ladderPriceDao.deleteByEntity(ladderPrice);
		CustomerPrice customerPrice = new CustomerPrice();
		customerPrice.setFixedExpenseId(fixedExpense);
		customerPriceDao.deleteByEntity(customerPrice);
		SupplierPrice supplierPrice = new SupplierPrice();
		supplierPrice.setFixedExpenseId(fixedExpense);
		supplierPriceDao.deleteByEntity(supplierPrice);
		VehicleSettlementInfo vehicleSettlementInfo = new VehicleSettlementInfo();
		vehicleSettlementInfo.setExpenseItemId(fixedExpense);
		vehicleSettlementInfoDao.deleteByEntity(vehicleSettlementInfo);
	}
	
}